#include<stdio.h>
#include<stdlib.h>
#define NUM 51
#define LINE 100
int main()
{
	int num, line, j, k, i;
	scanf("%d %d", &num, &line);
	while (num > 0 && num <= 50 && line > 0 && line <= 100)
	{
		struct order {
			int flag;
			char buf[NUM];
			int reverse;
		} boy[LINE];
		for (i = 0; i < line; i++)
		{
			boy[i].reverse = 0;
			boy[i].flag = i + 1;
			scanf("%s", boy[i].buf);
		}
		for (i = 0; i < line; i++)
			for (j = 0; j < num - 1; j++)
				for (k = j + 1; k < num; k++) {
					if (boy[i].buf[j] > boy[i].buf[k])
					{
						boy[i].reverse++;
					}
					else continue;
				}
		int cmp(const void *a, const void *b)
		{
			if (((struct order *)a)->reverse != ((struct order *)b)->reverse)
				return(((struct order *)a)->reverse > ((struct order *)b)->flag);
			else
				return(((struct order *)a)->flag - ((struct order *)b)->flag);
		}
		qsort(boy, line, sizeof(boy[0]), cmp);
		for (i = 0; i < line; i++)
		{
			printf("%s\n", boy[i].buf);
		}
		break;
	}
	return 0;
}